#ifndef __THREAD_HPP__
#define __THREAD_HPP__
#include<iostream>
#include<string>
#include<functional>
#include<pthread.h>
#include<unistd.h>
namespace yangbiao
{   
    template<typename T>
    using func_t = std::function<void(T)>;


    template<typename T>
    class Thread
    {
    public:
        Thread(func_t<T> func,T data,const std::string threadname="none-name"):
        _func(func),_data(data),_threadname(threadname),_stop(true)
        {}
        void Execute(){
            _func(_data);
        }
        static void* threadroutine(void* args){
           Thread<T>* self = static_cast<Thread<T>*>(args);
           self->Execute();
           return nullptr;
        }
        bool Start(){
            int n = pthread_create(&_tid,nullptr,threadroutine,this);
            if(n==0){
                _stop = false;
                return true;
            }else{
                return false;
            }
        }
        std::string name(){
            return _threadname;
        }
        void Detach(){
            if(_stop==false){
                pthread_detach(_tid);
            }
        }
        void Join(){
            if(_stop==false){
                pthread_join(_tid,nullptr);
            }
        }
        void Stop(){
            _stop=true;
        }
        ~Thread(){}
    private:
        pthread_t _tid;
        std::string _threadname;
        T _data;
        func_t<T> _func;
        bool _stop;

    };
}



#endif